import time
from datetime import datetime

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

# 创建浏览器驱动对象
driver=webdriver.Chrome()
# 窗口最大化显示
driver.maximize_window()
# 通过get方法打开页面
driver.get("http://localhost:82/index.php")
# 点击首页页面的登录按钮进入登录页面
driver.find_element(By.CSS_SELECTOR,'.red').click()
time.sleep(2)
# 在登录界面输入用户名和密码等点击登录按钮进入个人中心界面
driver.find_element(By.XPATH,'/html/body/div/div/div/div/form/div/div[1]/input').send_keys("13800138006")
time.sleep(2)
driver.find_element(By.XPATH,'/html/body/div/div/div/div/form/div/div[2]/input').send_keys("123456")
time.sleep(2)
driver.find_element(By.XPATH,'/html/body/div/div/div/div/form/div/div[3]/input').send_keys("8888")
time.sleep(2)
driver.find_element(By.XPATH,'/html/body/div/div/div/div/form/div/div/a').click()
time.sleep(2)
# 在个人中心界面点击首页
driver.find_element(By.CSS_SELECTOR,'body > div.nav-middan-z.p.home-index-head > div > div.m-navitems > ul > li:nth-child(1) > a').click()
time.sleep(2)
# 在首页点击搜索框输入小米后点击搜索按钮
driver.find_element(By.CSS_SELECTOR,'#q').send_keys("小米")
time.sleep(2)
driver.find_element(By.CSS_SELECTOR,'#searchForm > button').click()
time.sleep(2)
# 在搜索结果界面点击小米电视进入商品详情界面
driver.find_element(By.CSS_SELECTOR,'body > div.shop-list-tour.ma-to-20.p > div > div.stsho.fr > div.shop-list-splb.p > ul > li:nth-child(1) > div > div.xs_img > a > img').click()
time.sleep(2)
# 在商品详情界面把数量改成20
ele=driver.find_element(By.CSS_SELECTOR,'#number')
driver.execute_script('document.getElementById("number").value="20"',ele)
time.sleep(2)
# 点击购买按钮
driver.find_element(By.CSS_SELECTOR,'#buy_now').click()
time.sleep(2)
# 关闭提交订单页面的弹框
driver.switch_to.alert.accept()
# 获取收货人信息有几条收货人信息
count=len(driver.find_elements(By.XPATH,'//*[@id="ajax_address"]/div[2]/ul/li'))
# 在提交订单界面点击新增收货地址
driver.find_element(By.CSS_SELECTOR,'#addNewAddress').click()
time.sleep(2)
# 获取当前时间
now_time=datetime.now()
now_time=now_time.strftime('%Y%m%d%H%M%S')
# 获取收货人输入框元素
driver.find_element(By.CSS_SELECTOR,'input[name=consignee][type=text]').send_keys(f'cus{now_time}')
time.sleep(2)
# 获取手机号输入框并填写手机号
driver.find_element(By.XPATH,'//*[@id="address_form"]/div[2]/div/div[2]/div[2]/div/input').send_keys("15515166840")
time.sleep(2)
# 填写收获地址
select_province=Select(driver.find_element(By.CSS_SELECTOR,'#province'))
select_province.select_by_visible_text('河南省')
time.sleep(2)
select_city=Select(driver.find_element(By.CSS_SELECTOR,'#city'))
select_city.select_by_visible_text('安阳市')
time.sleep(2)
select_district=Select(driver.find_element(By.CSS_SELECTOR,'#district'))
select_district.select_by_visible_text('安阳县')
time.sleep(2)
select_twon=Select(driver.find_element(By.CSS_SELECTOR,'#twon'))
select_twon.select_by_visible_text('吕村镇')
time.sleep(2)
driver.find_element(By.XPATH,'//*[@id="address_form"]/div[2]/div/div[2]/div[4]/div/input').send_keys("吕村集村")
time.sleep(2)
driver.find_element(By.XPATH,'//*[@id="address_form"]/div[2]/div/div[2]/div[5]/div/input').send_keys('455113')
time.sleep(2)
driver.find_element(By.CSS_SELECTOR,'#address_submit').click()
time.sleep(2)
# 选择收货地址
driver.find_element(By.XPATH,'//*[@id="ajax_address"]/div[3]/span').click()
time.sleep(2)
# driver.find_element(By.XPATH,f'//*[@id="ajax_address"]/div[2]/ul/li[{count+1}]/div[1]').click()
driver.find_element(By.CSS_SELECTOR,'[id="ajax_address"] ul li:last-child div:first-child').click()
time.sleep(30)
# 收起地址
driver.find_element(By.XPATH,'//*[@id="ajax_address"]/div[3]/span').click()
# 输入备注信息
driver.find_element(By.XPATH,'//*[@id="express_delivery_modes"]/div[3]/textarea').send_keys("hurry up")
time.sleep(2)
# 操作滚动条到下面
driver.execute_script('window.scrollTo(0, 1000)')
time.sleep(2)
# 查询账户余额
balance=driver.find_element(By.XPATH,'//*[@id="usecou-step-tit"]/div/div[2]/div[3]/p[2]/label/span')
money=balance.text

time.sleep(2)
# 找到选择使用余额支付的选择框
use_money=driver.find_element(By.CSS_SELECTOR,'#user_money_checkbox')
# 添加鼠标驱动并点击选择框
ActionChains(driver).click(use_money).perform()
time.sleep(2)
# 选择输入框并输入金额
driver.find_element(By.CSS_SELECTOR,'#user_money').send_keys(money)
time.sleep(2)
# 输入支付密码
driver.find_element(By.CSS_SELECTOR,'#pay_pwd').send_keys("123456")
time.sleep(2)
# 获取商品总金额
all1=driver.find_element(By.XPATH,'/html/body/div[12]/div/div/div[1]/em')
total_amount=all1.text
total_amount=total_amount.split('￥')[1]
# 获取应付金额
all2=driver.find_element(By.CSS_SELECTOR,'#payables')
payable=all2.text
payable=payable.split('￥')[1]
# 将字符串类型的数据转换为小数
money=float(money)
total_amount=float(total_amount)
payable=float(payable)
# print(money,total_amount,payable)
assert payable==total_amount-money
# 点击提交订单按钮
driver.find_element(By.CSS_SELECTOR,'#submit_order').click()
time.sleep(5)
# 获取订单编号
order_number=driver.find_element(By.XPATH,'/html/body/div[2]/div/div[2]/div[1]/p')
order_num=order_number.text.split('|')[0].split('：')[1].strip()
print(order_number.text)







time.sleep(2)
# 关闭浏览器驱动对象
driver.quit()
